import { createRouter, createWebHistory } from 'vue-router';
import store from '@/store';

import indexRouter from '@/router/index/index';
import homeRouter from '@/router/home/index';

const routes = indexRouter.concat(homeRouter);

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = `${to.meta.title} - 明河`;
    }

    if (to.meta.requiredAuth && !store.getters.isAuthenticated) {
        next({ name: 'Login' });
    } else {
        next();
    }
});

export default router;
export { router as Router };
